package i.i.myapplication.tools;

import android.content.Context;

import com.google.android.exoplayer2.upstream.cache.LeastRecentlyUsedCacheEvictor;
import com.google.android.exoplayer2.upstream.cache.SimpleCache;

import java.io.File;
import java.io.IOException;

public class CacheManager {
    private static SimpleCache simpleCache;

    public static synchronized SimpleCache getInstance(Context context) {
        if (simpleCache == null) {
            File cacheDir = new File(context.getCacheDir(), "media_cache");
            long maxCacheSize = 100 * 1024 * 1024; // 100MB, 自行设置
            simpleCache = new SimpleCache(cacheDir, new LeastRecentlyUsedCacheEvictor(maxCacheSize));

        }
        return simpleCache;
    }

    public static synchronized void release() {
        if (simpleCache != null) {
            simpleCache.release();  // 这里不需要try-catch了
            simpleCache = null;
        }
    }
}
